﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Luna;

namespace TextDataExtractor.DEML.Filters {
	
	public class TrimFilter: DemlElement, IDataFilter {
		public object applyFilter(ExecutionContext context, object input) {
			if (input is string) {
				string s = (string)input;
				string op = getArgument("option").ToLower();
				
				if (string.IsNullOrWhiteSpace(op))
					op = "leftright";
				IList<string> options = getArgument("option").splitEx(',');
				if (Operation.isNotNullAndEmpty())
					options.Add(Operation.ToLower());
				if (options.Contains("trimleft"))
					s = s.TrimStart();
				if (options.Contains("trimright"))
					s = s.TrimEnd();
				if (options.Contains("trimall"))
					s = s.Trim();
				if (options.Contains("newline"))
					s = s.Replace("\n", "");
					
				input = s;
			}
			return input;
		}
	}
}
